﻿using System;

namespace awsomeSyntezator.PipeLine.Filters
{
	class MiddleCutOffFilter : Filter
	{
		public MiddleCutOffFilter(float cutOff, double reson) : base(cutOff, reson)
		{
			a0 = a * r;
			a1 = 0;
			a2 = -a0;
			b1 = -2 * c * r;
			b2 = (1 - a) * r;
		}
		public override void setCutOffFrequency(float cutOff)
		{
			cutOffFrequency = cutOff;
			s = Math.Sin((2.0 * Math.PI * cutOffFrequency) / (double)Options.rate);
			c = Math.Cos((2.0 * Math.PI * cutOffFrequency) / (double)Options.rate);
			a = s / (2 * resonance);
			r = 1 / (1 + a);

			a0 = a * r;
			a1 = 0;
			a2 = -a0;
			b1 = -2 * c * r;
			b2 = (1 - a) * r;

		}

		public override double getSample(int n, double sample)
		{
			double nowy = (a0 * sample + a1 * oldSample1 + a2 * oldSample2 - b1 * filtredSample1 - b2 * filtredSample2);

			oldSample2 = oldSample1;
			oldSample1 = sample;

			filtredSample2 = filtredSample1;
			filtredSample1 = nowy;

            return nowy;
		}
	}
}
